<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI助手 - 智能对话</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }

        .chat-container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 800px;
            height: 80vh;
            display: flex;
            flex-direction: column;
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            text-align: center;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        .header h1 {
            font-size: 24px;
            font-weight: 600;
            margin-bottom: 5px;
        }

        .header p {
            font-size: 14px;
            opacity: 0.9;
        }

        .chat-messages {
            flex: 1;
            overflow-y: auto;
            padding: 20px;
            background: #f8f9fa;
        }

        .message {
            margin-bottom: 20px;
            display: flex;
            align-items: flex-start;
            animation: fadeIn 0.3s ease-in;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .message.user {
            justify-content: flex-end;
        }

        .message-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
            margin: 0 10px;
            flex-shrink: 0;
        }

        .message.bot .message-avatar {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .message.user .message-avatar {
            background: #e3f2fd;
            color: #1976d2;
            order: 2;
        }

        .message-content {
            max-width: 70%;
            padding: 12px 16px;
            border-radius: 18px;
            word-wrap: break-word;
            position: relative;
        }

        .message.bot .message-content {
            background: white;
            border: 1px solid #e0e0e0;
            color: #333;
        }

        .message.user .message-content {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            order: 1;
        }

        .typing-indicator {
            display: none;
            padding: 12px 16px;
            background: white;
            border: 1px solid #e0e0e0;
            border-radius: 18px;
            margin-bottom: 20px;
            align-items: center;
        }

        .typing-indicator.active {
            display: flex;
        }

        .typing-indicator span {
            height: 8px;
            width: 8px;
            background-color: #667eea;
            border-radius: 50%;
            display: inline-block;
            margin: 0 2px;
            animation: typing 1.4s infinite ease-in-out;
        }

        .typing-indicator span:nth-child(2) {
            animation-delay: 0.2s;
        }

        .typing-indicator span:nth-child(3) {
            animation-delay: 0.4s;
        }

        @keyframes typing {
            0%, 60%, 100% {
                transform: translateY(0);
            }
            30% {
                transform: translateY(-10px);
            }
        }

        .input-container {
            padding: 20px;
            background: white;
            border-top: 1px solid #e0e0e0;
            display: flex;
            gap: 10px;
        }

        .input-field {
            flex: 1;
            padding: 12px 20px;
            border: 2px solid #e0e0e0;
            border-radius: 25px;
            font-size: 16px;
            outline: none;
            transition: all 0.3s ease;
        }

        .input-field:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .send-button {
            padding: 12px 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 25px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .send-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.3);
        }

        .send-button:active {
            transform: translateY(0);
        }

        .send-button:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .error-message {
            background: #ffebee;
            color: #c62828;
            padding: 10px 15px;
            border-radius: 10px;
            margin-bottom: 20px;
            display: none;
            animation: shake 0.5s ease-in-out;
        }

        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-10px); }
            75% { transform: translateX(10px); }
        }

        .empty-state {
            text-align: center;
            padding: 40px;
            color: #999;
        }

        .empty-state svg {
            width: 100px;
            height: 100px;
            margin-bottom: 20px;
            opacity: 0.3;
        }

        /* 滚动条样式 */
        .chat-messages::-webkit-scrollbar {
            width: 8px;
        }

        .chat-messages::-webkit-scrollbar-track {
            background: #f1f1f1;
        }

        .chat-messages::-webkit-scrollbar-thumb {
            background: #888;
            border-radius: 4px;
        }

        .chat-messages::-webkit-scrollbar-thumb:hover {
            background: #555;
        }

        /* 响应式设计 */
        @media (max-width: 600px) {
            .chat-container {
                height: 100vh;
                border-radius: 0;
            }

            .message-content {
                max-width: 85%;
            }
        }
    </style>
</head>
<body>
<div class="chat-container">
    <div class="header">
        <h1>🤖 AI助手</h1>
        <p>智能对话，实时响应</p>
    </div>

    <div class="chat-messages" id="chatMessages">
        <div class="empty-state" id="emptyState">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"/>
            </svg>
            <p>开始与AI助手对话吧！</p>
        </div>
    </div>

    <div class="typing-indicator" id="typingIndicator">
        <span></span>
        <span></span>
        <span></span>
    </div>

    <div class="error-message" id="errorMessage"></div>

    <div class="input-container">
        <input
                type="text"
                class="input-field"
                id="inputField"
                placeholder="输入您的问题..."
                autocomplete="off"
        >
        <button class="send-button" id="sendButton">
            <span>发送</span>
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <line x1="22" y1="2" x2="11" y2="13"></line>
                <polygon points="22 2 15 22 11 13 2 9 22 2"></polygon>
            </svg>
        </button>
    </div>
</div>

<script>
    class ChatApp {
        constructor() {
            this.chatMessages = document.getElementById('chatMessages');
            this.inputField = document.getElementById('inputField');
            this.sendButton = document.getElementById('sendButton');
            this.typingIndicator = document.getElementById('typingIndicator');
            this.errorMessage = document.getElementById('errorMessage');
            this.emptyState = document.getElementById('emptyState');

            this.initEventListeners();
        }

        initEventListeners() {
            this.sendButton.addEventListener('click', () => this.sendMessage());
            this.inputField.addEventListener('keypress', (e) => {
                if (e.key === 'Enter') {
                    this.sendMessage();
                }
            });
        }

        async sendMessage() {
            const question = this.inputField.value.trim();

            if (!question) {
                this.showError('请输入您的问题');
                return;
            }

            // 隐藏空状态
            if (this.emptyState) {
                this.emptyState.style.display = 'none';
            }

            // 添加用户消息
            this.addMessage(question, 'user');

            // 清空输入框
            this.inputField.value = '';
            this.inputField.disabled = true;
            this.sendButton.disabled = true;

            // 显示打字指示器
            this.showTypingIndicator();

            try {
                // 创建EventSource连接
                const eventSource = new EventSource(`/ai/generateStreamAsString?message=${encodeURIComponent(question)}`);

                let botMessage = null;
                let messageContent = '';

                eventSource.onmessage = (event) => {
                    if (!botMessage) {
                        this.hideTypingIndicator();
                        botMessage = this.addMessage('', 'bot');
                    }

                    messageContent += event.data;
                    botMessage.textContent = messageContent;

                    // 滚动到底部
                    this.scrollToBottom();
                };

                eventSource.onerror = (error) => {
                    eventSource.close();
                    this.hideTypingIndicator();

                    if (!botMessage) {
                        this.showError('连接失败，请稍后重试');
                    }

                    this.inputField.disabled = false;
                    this.sendButton.disabled = false;
                    this.inputField.focus();
                };

                eventSource.addEventListener('close', () => {
                    eventSource.close();
                    this.inputField.disabled = false;
                    this.sendButton.disabled = false;
                    this.inputField.focus();
                });

            } catch (error) {
                console.error('Error:', error);
                this.hideTypingIndicator();
                this.showError('发送失败，请稍后重试');
                this.inputField.disabled = false;
                this.sendButton.disabled = false;
                this.inputField.focus();
            }
        }

        addMessage(content, sender) {
            const messageDiv = document.createElement('div');
            messageDiv.className = `message ${sender}`;

            const avatar = document.createElement('div');
            avatar.className = 'message-avatar';
            avatar.textContent = sender === 'user' ? '👤' : '🤖';

            const contentDiv = document.createElement('div');
            contentDiv.className = 'message-content';
            contentDiv.textContent = content;

            messageDiv.appendChild(avatar);
            messageDiv.appendChild(contentDiv);

            this.chatMessages.appendChild(messageDiv);
            this.scrollToBottom();

            return contentDiv;
        }

        showTypingIndicator() {
            this.typingIndicator.classList.add('active');
            this.scrollToBottom();
        }

        hideTypingIndicator() {
            this.typingIndicator.classList.remove('active');
        }

        showError(message) {
            this.errorMessage.textContent = message;
            this.errorMessage.style.display = 'block';

            setTimeout(() => {
                this.errorMessage.style.display = 'none';
            }, 3000);
        }

        scrollToBottom() {
            this.chatMessages.scrollTop = this.chatMessages.scrollHeight;
        }
    }

    // 初始化应用
    document.addEventListener('DOMContentLoaded', () => {
        new ChatApp();
    });
</script>
</body>
</html>